/*
 * AlbumTableModel.java
 *
 * Created on June 11, 2005, 1:19 PM
 */
package com.bff.slimserver.musicjukebox.lib;

import com.bff.bjj.utils.Resources;
import com.bff.bjj.utils.Resources.LABEL_KEY;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.exception.SlimDatabaseException;
import org.bff.slimserver.musicobjects.SlimAlbum;
import org.bff.slimserver.musicobjects.SlimSong;

/**
 *
 * @author Bill
 */
public class AlbumTableModel extends SongTableModel {

    public static final int TITLE = 0;
    public static final int LENGTH = 1;
    public static final int TRACK = 2;
    public static final int YEAR = 3;
    public static final int BITRATE = 4;
    private HashMap<String, List<SlimSong>> groupMap;
    
    public final static String[] columnNames = {
        getHeader(LABEL_KEY.HEADER_TITLE),
        getHeader(LABEL_KEY.HEADER_LENGTH),
        getHeader(LABEL_KEY.HEADER_TRACK),
        getHeader(LABEL_KEY.HEADER_YEAR),
        getHeader(LABEL_KEY.HEADER_BITRATE)
    };

    /** Creates a new instance of TassTableModel */
    public AlbumTableModel(SlimAlbum album) throws SlimConnectionException, SlimDatabaseException {
        super(JukeboxProperties.getInstance().getDatabase().listSongsForAlbum(album));
        if (JukeboxProperties.getInstance().isGroupByFolder()) {
            groupByFolder();
        }
    }
   
    @Override
    public int getRowCount() {
        return (dataList.size());
    }

    @Override
    public Object getValueAt(int row, int col) {
        SlimSong song = dataList.get(row);
        switch (col) {
            case (TITLE):
                return getSongTitle(song);
            case (LENGTH):
                return song.getLength();
            case (TRACK):
                return song.getTrack();
            case (YEAR):
                return song.getYear();
            case (BITRATE):
                return (song.getBitrate());
            default:
                return (null);
        }
    }

    @Override
    public Class getColumnClass(int column) {
        if (column == TRACK) {
            return Integer.class;
        } else {
            return super.getColumnClass(column);
        }
    }

    @Override
    public int getColumnCount() {
        return (columnNames.length);
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getArtistColumnIndex() {
        return (-1);
    }

    @Override
    public int getAlbumColumnIndex() {
        return (-1);
    }

    @Override
    public int getTitleColumnIndex() {
        return (TITLE);
    }

    @Override
    public int getLengthColumnIndex() {
        return (LENGTH);
    }

    @Override
    public int getTrackColumnIndex() {
        return (TRACK);
    }

    @Override
    public int getYearColumnIndex() {
        return (YEAR);
    }

    @Override
    public int getBitrateColumnIndex() {
        return (BITRATE);
    }

    @Override
    public int getSortColumn() {
        return getTrackColumnIndex();
    }

    public List<String> getGroupList() {
        List<String> list = new ArrayList<String>(groupMap.keySet());
        Collections.sort(list);
        return list;
    }

    public void filterGroup(String directory) {
        if (groupMap.get(directory) != null) {
            this.dataList = groupMap.get(directory);
        }
    }

    private void groupByFolder() {
        groupMap = new HashMap<String, List<SlimSong>>();

        List<String> folders = new ArrayList<String>();
        for (SlimSong song : dataList) {
            int lastIndex = song.getUrl().lastIndexOf(Resources.FILE_SEPARATOR);
            String folder = song.getUrl().substring(0, lastIndex);
            if (!folders.contains(folder)) {
                folders.add(folder);
            }
        }

        if (folders.size() > 1) {
            for (String folder : folders) {
                List<SlimSong> songList = new ArrayList<SlimSong>();
                for (SlimSong song : dataList) {
                    int lastIndex = song.getUrl().lastIndexOf(Resources.FILE_SEPARATOR);
                    String songFolder = song.getUrl().substring(0, lastIndex);
                    if (folder.equals(songFolder)) {
                        songList.add(song);
                    }
                }
                groupMap.put(folder, songList);
            }
        }
    }
}
